package com.press.controllers.blog;

import com.alibaba.fastjson.JSONObject;
import com.press.bean.CommentBean;
import com.press.bean.ContentBean;
import com.press.bean.TaxonomyBean;
import com.press.beanforview.VContentBean;
import com.press.beanforview.VPageBean;
import com.press.controllers.admin.PanelController;
import com.press.services.ArticalTaxService;
import com.press.services.DetailsService;
import org.apache.log4j.Logger;
import org.apache.tools.ant.types.resources.comparators.Content;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

/**
 * Created by haocongshun on 2016/10/2.
 */
@Controller
@RequestMapping("/blog")
public class TaxonomyController {
    public static int nowPage;
    public static int pagecount;
    @Resource(name="ArticalTaxService")
    private ArticalTaxService articalTaxService;
    @Resource(name = "detailsService")
    private DetailsService detailsService;
    private static Logger log = Logger.getLogger(PanelController.class);
    @RequestMapping("/taxonomy")//得到分类的名称,并按照分类名称给每个分类名称绑定url
    public void taxonomy(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        UtilController utilController = (UtilController) WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()).getBean("utilController");
        utilController.contentsRecommend(request);
        utilController.contentsTop10(request);
        utilController.menus(request);

        int taxonomyID=0;
        String taxonomy=request.getParameter("tax");
        handlePage(request,response);
        log.info("*************tax:"+taxonomyID);
        if(taxonomy==null){
            Map<String,String> option = (Map<String ,String>)request.getServletContext().getAttribute("Option");
            String forwardUrl = "/"+option.get("web_template_id")+"/index.jsp";
            request.getRequestDispatcher(forwardUrl).forward(request,response);
        }else{
            taxonomyID=Integer.valueOf(taxonomy);
            String taxtitle=articalTaxService.getTitlebytaxonomyID(taxonomyID);
            List<ContentBean> contents=articalTaxService.getContentInformation(taxonomyID,(Integer.valueOf(nowPage)-1)*6,6);
            List<VContentBean> contentsTax=articalTaxService.setContentInVC(contents,request.getContextPath(),taxtitle,taxonomyID);
            request.setAttribute("contentsTax",contentsTax);
            request.setAttribute("taxtitle",taxtitle);

        }
        getContentPages(taxonomyID);//得到该分类下面的文章的总页数

       getPage(request,taxonomyID);

        Map<String,String> option = (Map<String ,String>)request.getServletContext().getAttribute("Option");
        String forwardUrl = "/"+option.get("web_template_id")+"/taxonomy.jsp";
        request.getRequestDispatcher(forwardUrl).forward(request,response);
    }


    @ModelAttribute
    public void init(){
        nowPage=1;
    }
    
    public int getContentPages(int taxid){//得到该分类id下面的所有符合条件的文章的页数
        int contentnumber=articalTaxService.getSameTaxContentNum(taxid);
        log.info("getContentPage得到分类id是"+taxid+"下面的同类文章的个数"+contentnumber);
        int connumber=contentnumber/6;
        if(contentnumber%6!=0){
            connumber++;
        }
        pagecount=connumber;
        return connumber;
    }
    public void handlePage(HttpServletRequest request, HttpServletResponse response){
        String page = request.getParameter("pn");
        log.info("得到的页数是第"+page);
        if(page==null){
            nowPage=1;
        }else if(page.equals("next")){
            if(nowPage<pagecount){
                nowPage++;
            }
        }else if(page.equals("last")){
            if(nowPage>1){
                nowPage--;
            }
        }else {
            log.info(page);
            nowPage=Integer.parseInt(page);
            log.info(nowPage);
        }
    }
    private void getPage(HttpServletRequest request,int tax) {
        List<VPageBean> vPages = new ArrayList<>();

        String urlPage = request.getServletContext().getAttribute("URLTaxonomy") + "?tax="+tax+"&pn=";

        VPageBean last = new VPageBean();
        last.setStatus("");
        last.setNum("<<");
        last.setUrl(urlPage+"last");
        vPages.add(last);

        for(int i = 1;i<=pagecount;i++){
            VPageBean vPage = new VPageBean();
            vPage.setNum(String.valueOf(i));
            vPage.setUrl(urlPage+i);
            if(i==nowPage){
                vPage.setStatus("active");
            }else {
                vPage.setStatus("");
            }
            vPages.add(vPage);
        }

        VPageBean next = new VPageBean();
        next.setStatus("");
        next.setNum(">>");
        next.setUrl(urlPage+"next");
        vPages.add(next);

        request.setAttribute("pages",vPages);

        request.setAttribute("pageCount",pagecount);

        request.setAttribute("pageNow",nowPage);
    }
    public DetailsService getDetailsService() {
        return detailsService;
    }

    public void setDetailsService(DetailsService detailsService) {
        this.detailsService = detailsService;
    }
}
